package com.zch.threaduse;

public class ThreadExit {
    public static void main(String[] args) throws InterruptedException {
        T t1 = new T();
        t1.start();

        //主线程休眠5秒，再通知 t1 线程退出
        System.out.println("main线程休眠5s");
        Thread.sleep(5000);
        t1.setLoop(false);
    }
}

class T extends Thread{
    private int count = 0;
    //设置一个控制变量
    private boolean loop = true;

    @Override
    public void run() {
        while (loop){
            try {
                Thread.sleep(900); //900ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("T 运行中..." + (++count));
        }
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}
